package com.test.webdemo.servlet;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
public class ControllerTrackingAspect {
    @Around("execution(* com.test.webdemo.controller..*.*(..))")
    public Object trackControllerMethod(ProceedingJoinPoint joinPoint) throws Throwable {
        String methodName = joinPoint.getSignature().toShortString();
        RequestTracker.logStep("5. 进入Controller方法: " + methodName);
        System.out.println("切面被调用，目标: " + joinPoint.getTarget().getClass().getName());
        // 记录方法参数
        Object[] args = joinPoint.getArgs();
        for (int i = 0; i < args.length; i++) {
            RequestTracker.log("参数[" + i + "]: " +
                    (args[i] != null ? args[i].getClass().getSimpleName() : "null"));
        }

        long start = System.currentTimeMillis();
        Object result = joinPoint.proceed();
        long duration = System.currentTimeMillis() - start;

        RequestTracker.logStep("7. 离开Controller方法 - 耗时: " + duration + "ms");

        if (result != null) {
            RequestTracker.log("返回结果类型: " + result.getClass().getSimpleName());
        }

        return result;
    }
}
